package com.wxlgroup.springboot.service.serviceImp;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wxlgroup.springboot.commons.Response;
import com.wxlgroup.springboot.dao.DepartmentDao;
import com.wxlgroup.springboot.dao.EquipmentDao;
import com.wxlgroup.springboot.entity.DElink;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;


@Service
public class EquipmentService implements com.wxlgroup.springboot.service.EquipmentService {


    @Resource
    private EquipmentDao equipmentDao;

    @Resource
    private DepartmentDao departmentDao;


    @Transactional  //开启事务,删除多个设备信息必须使用事务
    @Override
    public Response deleteEquipments(List<Integer> ids) {
        boolean flag=true;
        for (Integer id : ids) {
            int i = equipmentDao.deleteEquipmentById(id);
            if(i<0){
                flag=false;
            }
        }
        if(flag){
            return new Response("200",null,"批量删除成功");
        }else {
            return new Response("500",null,"批量删除失败");
        }

    }

    @Override
    public Response deleteEquipmentById(int id) {
        try {
            int i = equipmentDao.deleteEquipmentById(id);
            if(i>0){
                return new Response("200",null,"删除成功");
            }else {
                return new Response("500",null,"删除失败");
            }
        }catch (Exception e){
            return new Response("500",null,"没有此记录");
        }
    }


    @Override
    public Response updateEquipment(DElink dElink) {
            String department_name = dElink.getDepartment_name();
            int departmentId = departmentDao.queryDepartmentIdByName(department_name);
            boolean flag=false;
            DElink dElink1=null;
            if(dElink.getId()==0){     //无id说明是插入
                List<DElink> dElinks = equipmentDao.queryEquipmentIdByName(dElink.getEquipment_name());
                if(dElinks!=null){
                    for (DElink elink : dElinks) {
                        if(elink.getDepartment_name().equals(dElink.getDepartment_name())){
                            flag=true;
                            dElink1=elink;
                        }
                    }
                    if(flag){
                        dElink1.setEquipment_count(dElink1.getEquipment_count()+dElink.getEquipment_count());
                        int i = equipmentDao.updateEquipment(dElink1, dElink1.getDepartment_id());
                        if(i>0){
                            return new Response("200",null,
                                    dElink.getDepartment_name()+"已经有"+dElink.getEquipment_name()+"记录了,此次为修改操作");
                        }else {
                            return new Response("500",null,"插入错误");
                        }
                    }else {
                        equipmentDao.inertEquipmentIntoEquipment(dElink.getEquipment_name());
                        int i1 = equipmentDao.selectEquipmentIdByName(dElink.getEquipment_name());
                        dElink.setEquipment_id(i1);
                        dElink.setDepartment_id(departmentId);
                        int i = equipmentDao.insertEquipment(dElink);
                        if(i>0){
                            return new Response("200",null,"插入成功");
                        }else {
                            return new Response("500",null,"插入失败");
                        }
                    }
                }else {
                    equipmentDao.inertEquipmentIntoEquipment(dElink.getEquipment_name());
                    int i1 = equipmentDao.selectEquipmentIdByName(dElink.getEquipment_name());
                    dElink.setEquipment_id(i1);
                    dElink.setDepartment_id(departmentId);
                    int i = equipmentDao.insertEquipment(dElink);
                    if(i>0){
                        return new Response("200",null,"插入成功");
                    }else {
                        return new Response("500",null,"插入失败");
                    }
                }


            }else {                             //id不为空则为修改
                int i = equipmentDao.updateEquipment(dElink,departmentId);
                if(i>0){
                    return new Response("200",null,"修改成功");
                }else {
                    return new Response("500",null,"修改失败");
                }
            }
        }

    @Override
    public Object queryAllEquipments(int pageNumber,int pageSize,String equipmentName,String departmentName) {
        if(!"".equals(equipmentName)){
            equipmentName="%"+equipmentName+"%";
        }else {
            equipmentName="%%";
        }
        if(!"".equals(departmentName)){
            departmentName="%"+departmentName+"%";
        }else {
            departmentName="%%";
        }
        PageHelper.startPage(pageNumber, pageSize);
        List<DElink> dElinks = equipmentDao.queryAllEquipments(equipmentName,departmentName);
        PageInfo<DElink> pageInfo=new PageInfo<>(dElinks);
        return pageInfo;
    }
}
